package com.cskaoyan.gateway.controller.shopping;

/**
 * @author 1530009312@qq.com
 * @date Created in 2021/6/18 17:20
 * @description
 */

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/shopping")
@Api(tags = "CheckProductController", description = "查看、搜索商品")
public class CheckProductController {

    @Reference(timeout = 3000, check = false)
    IProductService productService;


    @GetMapping("/product/{id}")
    @ApiOperation("查看商品详情")
    public ResponseData checkProductDetail(HttpServletRequest httpServletRequest) {

        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        String requestURI = httpServletRequest.getRequestURI();
        //System.out.println(requestURI);
        String[] split = requestURI.split("/"); //以"/"截取字符串，分割出id
        //System.out.println(split[3]);
        productDetailRequest.setId(Long.valueOf(split[3]));
        ProductDetailResponse productDetail = productService.getProductDetail(productDetailRequest);

        if (productDetail.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {

            return new ResponseUtil().setData(productDetail.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(productDetail.getMsg());
    }


    @GetMapping("/goods")
    @ApiOperation("通过分页的方式查询所有商品")
    @Anoymous
    public ResponseData AllProductByPage(HttpServletRequest httpServletRequest) {

        String page = httpServletRequest.getParameter("page");
        String size = httpServletRequest.getParameter("size");
        String sort = httpServletRequest.getParameter("sort");
        String priceGt = httpServletRequest.getParameter("priceGt");
        String priceLte = httpServletRequest.getParameter("priceLte");
      //  System.out.println(page+" "+size+" "+sort+" "+priceGt+" "+priceLte);
        AllProductRequest allProductRequest = new AllProductRequest();
        allProductRequest.setPage(Integer.parseInt(page));
        allProductRequest.setSize(Integer.parseInt(size));
        allProductRequest.setSort(sort);

        if (!"".equals(priceGt)) {
            allProductRequest.setPriceGt(Integer.valueOf(priceGt));
        }
        if (!"".equals(priceLte)) {
            allProductRequest.setPriceLte(Integer.valueOf(priceLte));
        }

        AllProductResponse allProduct = productService.getAllProduct(allProductRequest);
        if (allProduct.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            AllProductResponse2 allProductResponse2 = new AllProductResponse2();
            allProductResponse2.setData(allProduct.getProductDtoList());
            allProductResponse2.setTotal(allProduct.getTotal());
//            ResponseUtil responseUtil = new ResponseUtil();
//            return  responseUtil.setDataAndTotal(allProduct.getProductDtoList(),allProduct.getTotal());
            return new ResponseUtil().setData(allProductResponse2);
        }
        return new ResponseUtil().setErrorMsg(allProduct.getMsg());
    }

    @GetMapping("/recommend")
    @ApiOperation("查询推荐商品")
    @Anoymous
    public ResponseData RecommendProduct() {

        RecommendResponse recommendGoods = productService.getRecommendGoods();
        if (recommendGoods.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(recommendGoods.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(recommendGoods.getMsg());
    }
}